package com.firstcards.web.util;

import java.io.StringWriter;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ResourceNotFoundException extends Exception {

	private static final Logger log = LoggerFactory.getLogger(ResourceNotFoundException.class);
	private static final long serialVersionUID = 8793185081138227977L;

	private String resourceName;
	private Map<String, String> resourceKeyMap;
	
	public ResourceNotFoundException(String resourceName, Map<String, String> resourceKeyMap){
		super(String.format("Resource type '%s' not found for keys %s", resourceName, formatKeys(resourceKeyMap)));
		this.resourceName = resourceName;
		this.resourceKeyMap = resourceKeyMap;
	}
	private static String formatKeys(Map<String, String> resourceKeyMap){
		ObjectMapper mapper = new ObjectMapper();
		StringWriter sw = new StringWriter();
		String retVal = null;
		try {
			mapper.writeValue(sw, resourceKeyMap);
			retVal = sw.toString();
		} catch (Exception e) {
			log.warn("Error while trying to JSON serialize resourceKeyMap");
			e.printStackTrace();
		}
		return retVal;
	}

	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	public Map<String, String> getResourceKeyMap() {
		return resourceKeyMap;
	}

	public void setResourceKeyMap(Map<String, String> resourceKeyMap) {
		this.resourceKeyMap = resourceKeyMap;
	}
	
	
}
